﻿<?php
	class ajaxPage 
	{
	 /*****
	 	@$limitNumber 每页条数
		@$initPage 初始第几页
		@$currentPage 当前第几页
		@$sumPages 总共多少页
		@$showNumber 分页显示多少个
		@$url 传值url
		@$pageArray 分页数组
		@$params 与数据库查询所需参数数组 如：array('a'=>1,'b'=>'erro')
	*/

		private $limitNumber;
		private  $currentPage;
		private  $sumPages;
		private  $showNumber;
		private  $url;
		private  $start;
		private  $end;
		public  $pageArray = array();
		public $params;
		private $startStr = '首页';
		private $endStr  = '尾页';
		private $nextStr  = '》';
		private $preStr = '《';
		private $jqueryFunction ;

		function __construct ($limitNumber,$sumNumber,$currentPage,$url,$showNumber,$params,$jqueryFunction='putCurrentPage'){
			$this->limitNumber = intval($limitNumber);
			$this->sumPages = ceil(intval($sumNumber)/intval($limitNumber));
			$this->currentPage = intval($currentPage);
			$this->url = $url;
			$this->showNumber = intval($showNumber);
			$this->jqueryFunction = $jqueryFunction;
			$this->params = $params;
			$this->initPageArray();
		}
		function __destruct()
		{
			unset($this->currentPage);
			unset($this->limitNumber);
			unset($this->sumPages);
			unset($this->url);
			unset($this->showNumber);
			unset($this->pageArray);
			unset($this->start);
			unset($this->end);
			unset($this->params);
			unset($this->startStr);
			unset($this->endStr);
			unset($this->nextStr);
			unset($this->preStr);
		}

		public function initPageArray()
		{

			for($i=1;$i<=$this->sumPages;$i++)
			{
				$this->pageArray[$i] = $i;
				// echo $this->pageArray[$i];
			}
		}

		public function showNumberPage()
		{

			(String) $showPageString='<div class="pageCss">';

				if($this->currentPage<=1)
				{
					$showPageString .= $this->getUnLink($this->startStr);
					$showPageString .= $this->getUnLink($this->preStr);
				}else{
					$showPageString .= $this->getLink($this->startStr,$this->getParams(1));
					//$showPageString .= '<div class="pageCssUnclick"><a href="javascript:void(0)" onclick="putCurrentPage('.($this->currentPage-1).')">《</a></div>';
					$showPageString .= $this->getLink($this->preStr,$this->getParams($this->currentPage-1));
				}
				if($this->sumPages<=$this->showNumber)
				{
				for($i=1;$i<=$this->sumPages;$i++)
					{	
						if($this->currentPage==$i)
						{
							//$showPageString .= '<div class="pageCssClick"><span>'.$this->pageArray[$i].'</span></div>';
							$showPageString .= $this->getUnLink($this->pageArray[$i]);
						}else{	/*echo $this->pageArray[$i];*/
							// $showPageString .= '<div class="pageCssUnclick"><a href="javascript:void(0)" onclick="putCurrentPage('.$this->pageArray[$i].')">'.$this->pageArray[$i].'</a></div>';
							$showPageString .= $this->getLink($this->pageArray[$i],$this->getParams($this->pageArray[$i]));
						}

					}
				}else{
					$this->start = $this->currentPage-floor($this->showNumber/2);
					$this->end = $this->currentPage+floor($this->showNumber/2);
					if($this->end >$this->sumPages	||	$this->currentPage>$this->sumPages-floor($this->showNumber/2))
					{
						$this->end = $this->sumPages;
						$this->start = $this->sumPages-2*floor($this->showNumber/2);
					}
					if($this->start<1)
					{
						$this->start = 1;
						$this->end = $this->showNumber;
					}
					for($i=$this->start;$i<=$this->end;$i++)
						{
							if($this->currentPage==$i)
							{
								$showPageString .= $this->getUnLink($this->pageArray[$i]);
							}else{
								$showPageString .= $this->getLink($this->pageArray[$i],$this->getParams($this->pageArray[$i]));
							}

						}
				}
				if($this->currentPage<$this->sumPages )
				{
					$showPageString .= $this->getLink($this->nextStr,$this->getParams($this->currentPage+1));
					$showPageString .= $this->getLink($this->endStr,$this->getParams($this->pageArray[$this->sumPages]));
				}else{
					$showPageString .= $this->getUnLink($this->nextStr);
					$showPageString .= $this->getUnLink($this->endStr);
				}


				$showPageString .='</div>';
			return $showPageString;
		}

		public function getUnLink($varchar)
		{
			return '<div class="pageCssUnclick"><span>'.$varchar.'</span></div>';
		}
		public function getLink($varchar,$params)
		{
			return '<div class="pageCssUnclick"><a href="javascript:void(0)" onclick='.$this->jqueryFunction.'('.json_encode($params).')>'.$varchar.'</a></div>';
		}

		public function getParams($addParam)
		{
			 $this->params['currentPage'] = $addParam;
			 return $this->params;
		}

		public function setStartStr($str )
		{
			$this->startStr = $str;
		}
		public function setEndStr($str)
		{
			$this->endStr = $str;
		}
		public function setNextStr($str)
		{
			$this->nextStr = $str;
		}
		public function setPreStr($str )
		{
			$this->preStr = $str;
		}


	}
?>